<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');
session_start(); //we need to call PHP's session object to access it through CI

class Home_Controller extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model('result_model', '', 'TRUE');
        $this->load->model('user', '', true);

        if (!$this->session->userdata('logged_in')) {
            redirect('login', 'refresh');
        }

        $this->load->library('authentication');
        $this->load->helper('t');
        $this->loggedUser = $this->authentication->getLoggedUser();
        $this->authentication->redirectIfNotBe(array('RSM', 'KAM'));
    }

    function index() {
        $this->load->model('evaluation');
        $this->load->model('attitudedetailresult');
        
        // load danh sách ss
        $ssList = $this->user->get_list_by_title(array('SS', 'KAS'), array('status' => 'active', 'rsm_code' => $this->loggedUser->code));
        
        
        // truy vấn số nhân viên đã đánh giá, số nhân viên đã confirm
        $evaluation = $this->evaluation->get_last();
        
        $tongDaDanhGia = 0;
        $tongDaXacNhan = 0;
        $tongNhanVien = 0;
        foreach ($ssList as &$eachSS) {
            $daDanhGia = 0;
            $daXacNhan = 0;
            $chuaDanhGia = 0;
            $users = $this->user->get_list_by_title(get_employee_titles(), array('supervisor_code' => $eachSS['code']));
            foreach ($users as $eachUser) {
                $attResult = $this->attitudedetailresult->get_wheres(array('user_id' => $eachUser['id'], 'eid' => $evaluation['id']));                
                if($attResult != false)
                {
                    $daDanhGia++;
                    if($attResult['status'] == 'confirmed')
                    {
                        $daXacNhan++;
                    }
                }
                else
                {
                    $chuaDanhGia++;
                }
            }
            $eachSS['progress'] = $daDanhGia.' / '.($daDanhGia+$chuaDanhGia);
            $eachSS['progress2'] = $daXacNhan.' / '.($daDanhGia+$chuaDanhGia);
            $tongDaDanhGia += $daDanhGia;
            $tongDaXacNhan += $daXacNhan;
            $tongNhanVien += $chuaDanhGia+$daDanhGia;
        }
        $eachSS['totalProgress'] = $tongDaDanhGia.' / '.($tongNhanVien);
        $eachSS['totalProgress2'] = $tongDaXacNhan.' / '.($tongNhanVien);
        
        
        // load todo list
        $this->load->model('evaluation');
        $e = $this->evaluation->get_last();

        $this->load->model('todo');
        $todoList = $this->todo->get_list_by_title_and_evaluation(strtolower($this->loggedUser->title), $e['id']);
        foreach ($todoList as &$each) {
            if (time() < strtotime($each['start_day'])) {
                $each['status'] = 'coming';
            } else if (time() <= strtotime($each['end_day'])) {
                $each['status'] = 'current';
            } else {
                $each['status'] = 'done';
            }
        }
        
        
        
        $viewData['todoList'] = $todoList;
        $viewData['ssList'] = $ssList;

        $this->load->view(get_folder($this->loggedUser->title) . '/home_view', $viewData);
    }

    function settings() {
        
        
        $this->load->view(get_folder($this->loggedUser->title) . '/settings', $viewData);
    }

    function logout() {
        $this->load->helper('cookie');
        $this->session->unset_userdata('logged_in');
        $institute_id = $this->session->userdata('institute_id');
        $this->session->sess_destroy();
        session_destroy();
        delete_cookie("rid");
        redirect('login/index/' . urlencode(base64_encode($institute_id)), 'refresh');
    }

}
?>


